package com.leyou.item.web;

import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.service.CategoryService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    /**
     * 根据父类目id查询子类目的集合
     * @param pid 父类目的id
     * @return 子类目的集合
     */
    @GetMapping("/of/parent")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByParentId(@RequestParam("pid") Long pid){

        List<CategoryDTO> categoryDTOList = categoryService.queryCategoryByParentId(pid);
        return ResponseEntity.ok(categoryDTOList);
    }

    /**
     * 根据id查询分类
     * @param id 分类id
     * @return 分类对象
     */
    @GetMapping("/{id}")
    public ResponseEntity<CategoryDTO> queryCategoryById(@PathVariable("id") Long id){
        CategoryDTO categoryDTO = categoryService.queryCategoryById(id);
        return ResponseEntity.ok(categoryDTO);
    }

    /**
     * 根据分类id集合查询分类集合  前端传的是字符串
     * @param ids
     * @return
     */
    @GetMapping("/list")       //@RequestParam("ids") List<Long> ids 前端传的是字符串，spring会自动切割转化为数组
    public ResponseEntity<List<CategoryDTO>> queryCategoryByIds(@RequestParam("ids") List<Long> ids){

        List<CategoryDTO> categoryDTOList = categoryService.queryCategoryByIds(ids);
        return ResponseEntity.ok(categoryDTOList);
    }

    /**
     * 根据品牌id查询分类集合
     * @param brandId 品牌id
     * @return 分类集合
     */
    @GetMapping("/of/brand")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByBrandId(@RequestParam("id") Long brandId) {
        List<CategoryDTO>categoryDTOList = categoryService.queryCategoryByBrandId(brandId);
        return ResponseEntity.ok(categoryDTOList);
    }

    /**
     * 获取分类目录 1,2,3级
     * @return
     */
    @GetMapping("/getMenuTree")
    public ResponseEntity<List<CategoryDTO>> getMenuTree(){
        List<CategoryDTO> menu = categoryService.getMenuTree();
        return ResponseEntity.ok(menu);
    }

    /**
     * 获取分类目录 1,2,3级
     * @return
     */
    @GetMapping("/findAll")
    public ResponseEntity<String> getIndexMenuTree(){
        return ResponseEntity.ok(categoryService.getIndexMenuTree());
    }
}
